package com.whnk.evo.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.whnk.common.config.Global;
import com.whnk.common.support.Convert;
import com.whnk.evo.domain.ModInfo;
import com.whnk.evo.domain.SealInfo;
import com.whnk.evo.domain.VoucherInfo;
import com.whnk.evo.mapper.ModInfoMapper;
import com.whnk.evo.mapper.SealInfoMapper;
import com.whnk.evo.mapper.VoucherInfoMapper;
import com.whnk.evo.service.IVoucherInfoService;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

/**
 * 电子凭证 服务层实现
 * 
 * @author whwhnk
 * @date 2019-09-03
 */
@Service
public class VoucherInfoServiceImpl implements IVoucherInfoService 
{
	private final Logger logger = LoggerFactory.getLogger(VoucherInfoServiceImpl.class);

	@Autowired
	private VoucherInfoMapper VoucherInfoMapper;
	
	@Autowired
	private ModInfoMapper modInfoMapper;
	
	@Autowired
	private SealInfoMapper sealInfoMapper;

	private Gson gson = new Gson();

	/**
     * 查询电子凭证信息
     * 
     * @param id 电子凭证ID
     * @return 电子凭证信息
     */
    @Override
	public VoucherInfo selectVoucherInfoById(Integer id)
	{
	    return VoucherInfoMapper.selectVoucherInfoById(id);
	}
	
	/**
     * 查询电子凭证列表
     * 
     * @param VoucherInfo 电子凭证信息
     * @return 电子凭证集合
     */
	@Override
	public List<VoucherInfo> selectVoucherInfoList(VoucherInfo VoucherInfo)
	{
	    return VoucherInfoMapper.selectVoucherInfoList(VoucherInfo);
	}
	
    /**
     * 新增电子凭证
     * 
     * @param VoucherInfo 电子凭证信息
     * @return 结果
     */
	@Override
	public int insertVoucherInfo(VoucherInfo VoucherInfo)
	{
	    return VoucherInfoMapper.insertVoucherInfo(VoucherInfo);
	}
	
	/**
     * 修改电子凭证
     * 
     * @param VoucherInfo 电子凭证信息
     * @return 结果
     */
	@Override
	public int updateVoucherInfo(VoucherInfo VoucherInfo)
	{
	    return VoucherInfoMapper.updateVoucherInfo(VoucherInfo);
	}

	/**
     * 删除电子凭证对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteVoucherInfoByIds(String ids)
	{
		return VoucherInfoMapper.deleteVoucherInfoByIds(Convert.toStrArray(ids));
	}

	/**
	 * 根据凭证ID查询模板ID，生成凭证PDF
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public String expEvoById(Integer id) throws JRException, IOException {
		// 1.根据ID查询模板全路径名称，以及测试数据。
		VoucherInfo VoucherInfo = VoucherInfoMapper.selectVoucherInfoById(id);
		ModInfo modInfo = null;
		List<SealInfo> seals = null;
		if(null != VoucherInfo && null != VoucherInfo.getModId()) {
			modInfo = modInfoMapper.selectModInfoById(VoucherInfo.getModId());
			seals = sealInfoMapper.querySealsByModId(VoucherInfo.getModId());
		}
		
		String filePath = modInfo.getModFilepath(); // 模板路径
		String testData = modInfo.getModNote(); // 测试数据 json
		
		// 模板的底板
		@SuppressWarnings("unchecked")
		Map<String,Object> dataMap = gson.fromJson(testData, Map.class);
		dataMap.put("bgimg", modInfo.getModBgFilepath());

		logger.info("######filePath=" + filePath);
		logger.info("######testData=" + testData);
		
		// 2.公章名称和全路径名
		for(SealInfo sealInfo : seals) {
			// sealNm为ireport中定义的变量名，全路径名为该变量的值。
			logger.info("######sealNm=" + sealInfo.getSealNm()+"######sealFilepath=" + sealInfo.getSealFilepath());
			dataMap.put(sealInfo.getSealNm(),sealInfo.getSealFilepath());
		}
		
		// 3.使用JasperReport组装PDF，生成PDF文件
		File fileJasper = new File(filePath);
		JasperReport report = (JasperReport) JRLoader.loadObject(fileJasper);
		JasperPrint jasperPrint = JasperFillManager.fillReport(report, dataMap, new JREmptyDataSource());
		String fileNamePDF = VoucherInfo.getEvoNm() + VoucherInfo.getModNm() + ".pdf";
		String filePathPDF = Global.getDownloadPath();
		File file = new File(filePathPDF);
		file.mkdirs();
		JasperExportManager.exportReportToPdfFile(jasperPrint, filePathPDF + fileNamePDF);
		return fileNamePDF;
	}
	
}
